package com.utils.info.controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.utils.info.util.hhy.DownloadPDFUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class outPDFController {

    @GetMapping("/pdf")
    public void testPDF(HttpServletResponse resp){

        List<String> tests = new ArrayList<>();

        for (int i = 1; i <= 20; i++) {
            tests.add(i+"");
        }

        Document document = null;
        PdfWriter writer = null;
        try {
            document = new Document(PageSize.A4);
            writer = PdfWriter.getInstance(document,resp.getOutputStream());

            document.open();

            BaseFont bfChinese = BaseFont.createFont("C:\\Users\\admin\\Desktop\\image\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            DownloadPDFUtil.setTitle(document,bfChinese,"测试标题",20);
            DownloadPDFUtil.setHeading(document,bfChinese,"测试小标题",10,16);
            for (String test : tests) {
                Font font = new Font(bfChinese, 14, Font.BOLD); //设置字体
                Paragraph paragraph = new Paragraph(test, font); //标题
                document.add(paragraph);
            }
            DownloadPDFUtil.setTable(document,bfChinese,5,14,tests);
            String url = "C:\\Users\\admin\\Desktop\\image\\";
            List<String> images = new ArrayList<>();
            images.add("gongShi1.png");
            images.add("gongShi2.png");
            images.add("shuoMing.png");
            DownloadPDFUtil.setLocalImages(document,1,100,url,images,null,null);

            List<String> urls = new ArrayList<>();
            urls.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F945cad7c7105d352aa3c513c94846bb0c2f1a61342067-OWFZcE_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638609744&t=74beccfc0e47d41a47d7456d75667b26");
            urls.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F53%2F0a%2Fda%2F530adad966630fce548cd408237ff200.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638609822&t=be8ea4821cbb570bb25a8920264fe8ed");
            DownloadPDFUtil.setNetworkImages(document,2,100,urls,240f,120f);

//            DownloadPDFUtil.export(document,writer,"测试",resp);
            //处理乱码
            resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
            resp.addHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("测试", "UTF-8") + ".pdf");
            //下载刷新
            writer.flush();
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
